package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;



@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;   //找到匹配的方法insert
    public void start() throws SQLException {
//        System.out.println("UserController :开始处理用户请求");
//        System.out.println("UesrConller : 开始处理注册");
//        System.out.println("UserConller : 获取用户输入信息");
//        userMapper.insert(null);    //找到匹配的方法
//        System.out.println("UserController:注册成功！");
        Scanner scan =new Scanner(System.in);
        System.out.println("请输入编号");
        System.out.println("1:用户注册");
        System.out.println("2:用户登录");
        System.out.println("3:用户列表");
        System.out.println("4:修改用户信息");
        int num =scan.nextInt();
        switch (num){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
        }

    }

    private int  userUpdate() {
        System.out.println("开始修改信息");
        User user =new User();
        Scanner scaner =new Scanner(System.in);
        System.out.println("请输入用户名");
        user.setUsername(scaner.nextLine());
        System.out.println("请输入密码");
        user.setPassword(scaner.nextLine());
        System.out.println("请输入昵称");
        user.setNickname(scaner.nextLine());
        System.out.println(user);
        int result = userMapper.update(user);
        System.out.println(result>0?"修改成功":"修改失败");
        return result;

    }

    private void userReg() throws SQLException {
        System.out.println("开始注册");
        User user =new User();
        Scanner scaner =new Scanner(System.in);
        System.out.println("请输入用户名");
        user.setUsername(scaner.nextLine());
        System.out.println("请输入密码");
        user.setPassword(scaner.nextLine());
        System.out.println("请输入昵称");
        user.setNickname(scaner.nextLine());
        System.out.println(user);
        if(userMapper.query(user.getUsername(), user.getPassword()) == null){
            // Check if user already exists
            int result =userMapper.insert(user);
            System.out.println("注册成功");
        }else{
            System.out.println("该用户 【"+user.getUsername()+"】已存在");
        }

        int result = userMapper.insert(user);
        System.out.println(result>0?"注册成功":"注册失败");
    }
    private void userLogin() throws SQLException {
        User user =new User();
        System.out.println("开始登录");
        Scanner scanner =new Scanner(System.in);
        System.out.println("请输入用户名");
        user.setUsername(scanner.nextLine());
        System.out.println("请输入密码");
        user.setPassword(scanner.nextLine());
        User result = userMapper.query(user.getUsername(), user.getPassword());
        System.out.println(result);
        if(result != null){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }


    }
    private void userList(){
        System.out.println("展示用户列表");
        //将所有用户查询出来，并输出到控制台上
        List<User> userList = userMapper.selectAll();
        for(User user:userList){
            System.out.println(user);
        }
    }


}
